﻿using Microsoft.FxCop.Sdk;

namespace Lephone.FxCop.Rules
{
    public class FunctionShouldLessThenLimitedLines : BaseIntrospectionRule
    {
        private readonly string ErrorMessage;

        public FunctionShouldLessThenLimitedLines()
            : base("FunctionShouldLessThenLimitedLines", "Lephone.FxCop.Rules.LephoneRules", typeof(FunctionShouldLessThenLimitedLines).Assembly)
        {
            ErrorMessage = string.Format("Function Should Less Then {0} Lines", AppSettings.FunctionLinesLimitation);
        }

        public override ProblemCollection Check(Member member)
        {
            var method = member as Method;
            if (method != null && !string.IsNullOrEmpty(method.SourceContext.FileName))
            {
                var sc = CommonFunctions.GetSrcContext(method);
                var i = sc.EndLine - sc.StartLine;
                if (i > AppSettings.FunctionLinesLimitation)
                {
                    Problems.Add(new Problem(new Resolution(ErrorMessage)));
                    return Problems;
                }
            }
            return null;
        }

        public override TargetVisibilities TargetVisibility
        {
            get
            {
                return TargetVisibilities.All;
            }
        }
    }
}
